package st.business.tepth.business.user.busnumber.workholder.repair.finish;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.CompleteInfoBean;
import st.business.tepth.business.businfo.BusInfoDelegate;
import st.business.tepth.business.event.ReloadBusNumberDataEvent;
import st.business.tepth.business.event.ReloadRepairWorkDataEvent;
import st.business.tepth.business.event.StartBrotherEvent;
import st.business.tepth.business.user.busnumber.workholder.maintain.finish.DialogFinishNotPass;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-26 11:11
 * 小修竣检
 */

public class RepairCompleteDelegate extends MvpFragment<RepairCompletePresenter>
        implements RepairCompleteContract.View {
    public static RepairCompleteDelegate newInstance(int repairId,
                                                     int type,
                                                     CompleteInfoBean completeInfoBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("complete", completeInfoBean);
        bundle.putInt("type", type);
        bundle.putInt("repairId", repairId);
        RepairCompleteDelegate delegate = new RepairCompleteDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_work_type)
    TextView mTvType;
    @BindView(R2.id.tv_check_pass)
    TextView mTvCheckPass;
    @BindView(R2.id.tv_check_not_pass)
    TextView mTvCheckNoPass;
    @BindView(R2.id.tv_cancel_work)
    TextView mTvCancelWork;
    private CompleteInfoBean mCompleteInfoBean;
    private RepairCompleteAdapter mAdapter;
    private int mType;
    private DialogLoader mDialogLoader;
    private int mRepairId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mCompleteInfoBean = bundle.getParcelable("complete");
            mType = bundle.getInt("type");
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public RepairCompletePresenter getPresenter() {
        return new RepairCompletePresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_repair_complete;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        String type = "";
        switch (mType) {
            case 1:
                type = "小修";
                break;
            case 2:
                type = "一级保养";
                break;
            case 3:
                type = "二级保养";
                break;
            case 4:
                type = "三级保养";
                break;
            case 5:
                type = "电池保养";
                break;
        }
        mTvType.setText(String.format("%s--待竣检", type));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        if (mCompleteInfoBean != null) {
            if (mCompleteInfoBean.getCompleteList() != null) {
                mAdapter = new RepairCompleteAdapter(
                        RepairCompleteDataConvert.
                                convert(mCompleteInfoBean));
                mRecyclerView.setAdapter(mAdapter);
            }
            if (mCompleteInfoBean.getOperationState() == 0) {
                mTvCheckPass.setVisibility(View.INVISIBLE);
                mTvCheckNoPass.setVisibility(View.INVISIBLE);
            } else {
                mTvCheckPass.setVisibility(View.VISIBLE);
                mTvCheckNoPass.setVisibility(View.VISIBLE);
            }
            mTvCancelWork.setVisibility(mCompleteInfoBean.isWorker() ?
                    View.VISIBLE : View.GONE);
        }
        mDialogLoader = new DialogLoader(_mActivity);
    }

    @OnClick(R2.id.tv_check_pass)
    public void checkPass() {
        mDialogLoader.showProgressDialog(true, "正在提交", "请稍候...");
        mPresenter.complete(UserManager.getInstance().getToken(), mRepairId,
                "", "",
                0
        );
    }

    @OnClick(R2.id.tv_check_not_pass)
    public void checkNotPass() {
//        mDialogLoader.showProgressDialog(true, "正在提交", "请稍候...");
//        mPresenter.complete(UserManager.getInstance().getToken(), mRepairId, 1
//        );
        new DialogFinishNotPass(_mActivity, "竣检不通过", RepairCompleteDataConvert.convertToClassification(mCompleteInfoBean),
                new DialogFinishNotPass.OnConfirmListener() {
                    @Override
                    public void onConfirm(String ids, String reason) {
                        mDialogLoader.showProgressDialog(true, "正在提交", "请稍候...");
                        mPresenter.complete(UserManager.getInstance().getToken(),
                                mRepairId, ids, reason, 1
                        );
                    }
                }).measureSize().show();
    }

    @Override
    public void completePass() {
        mDialogLoader.stopProgressWithSuccess("提交成功", null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                EventBus.getDefault().post(new ReloadBusNumberDataEvent());
            }
        });
    }

    @OnClick(R2.id.tv_bus_info)
    public void onShowBusInfo() {
        EventBus.getDefault().post(new StartBrotherEvent(BusInfoDelegate.newInstance(mRepairId)));
    }

    @Override
    public void completeNotPass() {
        mDialogLoader.stopProgressWithSuccess("提交成功", null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                EventBus.getDefault().post(new ReloadRepairWorkDataEvent(mRepairId));
            }
        });
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed("竣检失败", msg);
    }

    @OnClick(R2.id.tv_cancel_work)
    public void onWorkCancel() {
        mDialogLoader.showProgressDialog(false, "正在提交", "请稍候...");
        mPresenter.cancelFinishWork(UserManager.getInstance().getToken(),
                mRepairId);
    }

    @Override
    public void cancelWorkSucceed() {
        mDialogLoader.stopProgressWithSuccess("提交成功", null, new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                EventBus.getDefault().post(new ReloadRepairWorkDataEvent(mRepairId));
            }
        });
    }
}